package com.woniuxy.controller;

import com.nimbusds.jose.JOSEException;
import com.woniuxy.entity.DTO.*;
import com.woniuxy.service.CourierService;
import com.woniuxy.util.MinioUtil;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import com.woniuxy.util.UserContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/courier")
public class CourierController {
    @Resource
    private CourierService courierService;

    @Resource
    private MinioUtil minioUtils;

    @GetMapping("/queryRiderList")
    public ResponseData queryRiderList() {
        List<CourierDTO> courierDTOS = courierService.queryCourierDTO();
        return ResponseData.success(courierDTOS);
    }

    @PostMapping("/saveAddCourier")
    public ResponseData saveAddCourier(@RequestBody CourierDTO courierDTO) {
        Integer integer = courierService.updateCourierById(courierDTO.getId(), courierDTO.getWarehouseId());
        return ResponseData.success(integer);
    }

    @GetMapping("/queryRiderByCondition/{warehouseId}")
    public ResponseData queryRiderByCondition(@PathVariable Integer warehouseId) {
        List<CourierDTO> courierDTOS = courierService.queryCourierDTOByWarehouseId(warehouseId);
        return ResponseData.success(courierDTOS);
    }

    @PostMapping("/queryRiderByName")
    public ResponseData queryRiderByName(@RequestBody CourierDTO courierDTO) {
        List<CourierDTO> courierDTOS = courierService.queryRiderByName(courierDTO.getCourierName(), courierDTO.getWarehouseId());
        return ResponseData.success(courierDTOS);
    }

    @GetMapping("/riderUnbind/{id}")
    public ResponseData riderUnbind(@PathVariable Integer id) {
        courierService.riderUnbind(id);
        return ResponseData.success();
    }

    @PostMapping("/register")
    public ResponseData register(@RequestBody CourierDTO courierDTO) {
        System.out.println("111");
        boolean b = courierService.queryCourierByAccount(courierDTO.getCourierAccount());
        if (b) {
            return ResponseData.fail(ResponseEnum.ACCOUNT_EXISTS_FAIL);
        }
        boolean register = courierService.register(courierDTO);
        return ResponseData.success(register);
    }

    @PostMapping("/login")
    public ResponseData login(@RequestBody CourierDTO courierDTO) throws JOSEException {
        System.out.println("收到登录请求："+ courierDTO);
        ResponseData login = courierService.login(courierDTO);
        if(login.getCode()==ResponseEnum.SUCCESS.getCode()){
            return ResponseData.success(login);
        }else {
            return ResponseData.fail(ResponseEnum.LOGIN_FAIL);
        }

    }

    @GetMapping("/profile")
    public ResponseData getProfile(@RequestParam("courierAccount") String courierAccount) throws ParseException {
        // 通过 UserContext 获取当前用户ID
        Integer userId = UserContext.getUserId();
        CourierDTO courierDTO = courierService.queryProfileById(userId);
        return ResponseData.success(courierDTO);
    }

    @GetMapping("/warehouse-status")
    public ResponseData getWarehouseStatus() throws ParseException {
        // 通过 UserContext 获取当前用户ID
        Integer userId = UserContext.getUserId();

        CourierDTO courierDTO = courierService.queryProfileById(userId);
        Integer warehouseId = courierDTO.getWarehouseId();
        if(warehouseId!=null){
            return ResponseData.success(warehouseId);
        }else {
            return ResponseData.fail(ResponseEnum.UNBOUND_WAREHOUSE);
        }
    }


    @PostMapping("/changePassword")
    public ResponseData changePassword(@RequestBody ChangePasswordDTO changePasswordDTO) throws ParseException {
        // 通过 UserContext 获取当前用户ID
        Integer userId = UserContext.getUserId();
        changePasswordDTO.setId(userId);
        boolean b = courierService.updatePasswordById(changePasswordDTO);
        return ResponseData.success(b);
    }

    @GetMapping("/available-orders")
    public ResponseData availableOrder() throws ParseException {
        // 通过 UserContext 获取当前用户ID
        Integer userId = UserContext.getUserId();
        //获取该仓库下的所有配送订单
        CourierDTO courierDTO = courierService.queryProfileById(userId);
        Integer warehouseId = courierDTO.getWarehouseId();
        List<DeliveryOrderDTO> deliveryOrderDTOS = courierService.queryAvailableOrders(warehouseId);
        return ResponseData.success(deliveryOrderDTOS);
    }

    @PostMapping("/receive-orders")
    public ResponseData receiveOrder(@RequestBody List<String> orderIds) throws ParseException {
        // 通过 UserContext 获取当前用户ID
        Integer userId = UserContext.getUserId();

        System.out.println(orderIds);
        Integer integer = courierService.receiveOrder(orderIds, userId);

        return ResponseData.success(integer);
    }

    @GetMapping("/my-orders")
    public ResponseData myOrders() throws ParseException {
        // 通过 UserContext 获取当前用户ID
        Integer userId = UserContext.getUserId();
        List<DeliveryOrderDTO> deliveryOrderDTOS = courierService.queryMyOrders(userId);
        return ResponseData.success(deliveryOrderDTOS);
    }

    @PostMapping("/complete-order")
    public ResponseData completeOrder(@RequestBody String orderId) throws ParseException {
        // 通过 UserContext 获取当前用户ID
        Integer userId = UserContext.getUserId();
        System.out.println(orderId);
        boolean b = courierService.completeOrder(orderId);
        return ResponseData.success(b);
    }

    @GetMapping("/dashboardDay")
    public ResponseData getDashboardDataDay(
            @RequestParam(value = "timeType", defaultValue = "day") String timeType) throws ParseException {
        // 通过 UserContext 获取当前用户ID
        Integer userId = UserContext.getUserId();
        List<PerformanceDTO> performanceDTOS = courierService.queryPerformance(userId);
        return ResponseData.success(performanceDTOS);
    }

    @GetMapping("/dashboardMonth")
    public ResponseData getDashboardDataMonth(
            @RequestParam(value = "timeType", defaultValue = "month") String timeType) throws ParseException {
        // 通过 UserContext 获取当前用户ID
        Integer userId = UserContext.getUserId();
        List<PerformanceDTO> performanceDTOS = courierService.queryMonthPerformance(userId);
        return ResponseData.success(performanceDTOS);
    }

    @PostMapping("/imgLoad")
    public ResponseData imgLoad(MultipartFile file, HttpServletRequest request){
        try {
            String imgPath = minioUtils.uploadFile(file, "image");
            Integer userId = UserContext.getUserId();
            courierService.imgLoad(userId, imgPath);
            return ResponseData.success(imgPath);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
